<?php 
require 'db.php';
//==================Hàm code tự sinh mã tự động==============
//<?php
/*
$sql=mysql_fetch_assoc(mysql_query("select count(*) as mamh from dbo_monhoc"));
$stmt = $db->prepare($sql);
$dem2=$dem1['dem'];
if($dem2<10)
{$MT='MT00';
$add="$MT$dem2";
}
if($dem2>=10 and $dem2<=99)
{$MT='MT0';
$add="$MT$dem2";
}
if($dem2>=100 )
{$MT='MT';
$add="$MT$dem2";
}
mysql_query("insert into table value('$add')");
*/
?>
<?php
//==================Hàm xóa==================================
if(isset($_POST['manhom']))$manhom=$_POST['manhom'];
if(isset($_POST['masv']))$masv=$_POST['masv'];
if(isset($_POST['del'])){
	$sql = "DELETE FROM dbo_dondangky WHERE MaSV=? and manhom=?";
	$stmt = $db->prepare ( $sql );
	$stmt->bindParam ( "1", $masv );
	$stmt->bindParam ( "2", $manhom );
	$stmt->execute ();
	$success=$stmt->rowCount();
	if(!$success){
		echo "Có lỗi xảy ra vui lòng thử lại sau ";
	}else{
		echo 1;
		exit;
	}
}
//====================================================
if(isset($_POST['AddMonTheThao'])){
		if (isset($_REQUEST["MaNhom"])){
			$maNhom = $_REQUEST["MaNhom"];
			$maSV = $_REQUEST["masv"];
			$sdtdk = $_REQUEST["sdtdk"];
			$MaMonTheThao = explode (",", $maNhom);
			foreach ( $MaMonTheThao as $mamtt ){
				if(!empty($mamtt)){
					$sql = "INSERT INTO dbo_dondangky(MaSV,MaNhom,ThanhTich,SDTDK) VALUES ('". $maSV. "','" . $mamtt . "','','".$sdtdk."')";
					$result = $db->exec( $sql );
				}
			}
 			echo "Đăng ký thành công";
			exit();
		}
	}
//====================================================
	if(isset($_POST['mamon'])) $mamon=$_POST['mamon'];
	$noidung = array();
	$sqldk="SELECT  dbo_dondangky.MANHOM,TENNHOM,SISOTOIDA,SISOTOIDA-count(MASV) as SVDKT,TENMH ";
	$sqldk.=" from dbo_noidung,dbo_dondangky,dbo_monhoc where dbo_noidung.MAMH=:mamh and dbo_noidung.MANHOM=dbo_dondangky.MANHOM and dbo_monhoc.MAMH=dbo_noidung.MAMH ";
	$sqldk.=" group by MANHOM";
	$sqldk.=" union (";
	$sqldk.=" SELECT  MANHOM,TENNHOM,SISOTOIDA,SISOTOIDA as SVDKT,TENMH ";
	$sqldk.=" from dbo_noidung,dbo_monhoc where dbo_noidung.MaMH=:mamh and dbo_monhoc.MAMH=dbo_noidung.MAMH  and  MANHOM not in ( ";
	$sqldk.=" SELECT  dbo_dondangky.MANHOM ";
	$sqldk.=" from dbo_noidung,dbo_dondangky,dbo_monhoc where dbo_noidung.MaMH=:mamh and dbo_noidung.MANHOM=dbo_dondangky.MANHOM and dbo_monhoc.MAMH=dbo_noidung.MAMH )) ";
	$stmt = $db->prepare($sqldk);
	$stmt->bindParam("mamh",$mamon,PDO::PARAM_STR);
	$stmt->execute();
	if($stmt->execute()){
	while($r=$stmt->fetch()){
		$noidung=array_merge($noidung,array(
				array("MANHOM"=>$r['MANHOM'],"TENNHOM"=>$r['TENNHOM'],"SISOTOIDA"=>$r['SISOTOIDA'],"SVDKT"=>$r['SVDKT'],"TENMH"=>$r['TENMH'])
		));
	}}


//=================Xay dung ham kiem tra ==================================
	$kiemtra = array();
	$mamon=$_POST['mamon'];
	if(isset($_POST['masv']))$masv=$_POST['masv'];
	$sqlkt="SELECT  dbo_dondangky.MANHOM,TENNHOM,SISOTOIDA,SISOTOIDA-count(MASV) as SVDKT,TENMH,dbo_dondangky.SDTDK ";
	$sqlkt.=" from dbo_noidung,dbo_dondangky,dbo_monhoc";
	$sqlkt.=" where dbo_noidung.MAMH=:mamh and dbo_noidung.MANHOM=dbo_dondangky.MANHOM and dbo_monhoc.MAMH=dbo_noidung.MAMH and dbo_dondangky.MASV=:masv";
	$sqlkt.=" group by MANHOM";
	$stmtkt = $db->prepare($sqlkt);
	$stmtkt->bindParam('mamh',$mamon,PDO::PARAM_STR);
	$stmtkt->bindParam('masv',$masv,PDO::PARAM_STR);
	$stmtkt->execute();
	if($stmtkt->execute()){
		while($rkt=$stmtkt->fetch()){
			$kiemtra=array_merge($kiemtra,array(
					array("MANHOM"=>$rkt['MANHOM'])
			));
		}}

//==================================================== 
?>
<div id="khungmonthi">
  <table  class="table table-bordered table-hover" style="font-size: 13px;">
    <thead>
      <tr class="info">
	  	<th>STT</th>  
		<th>Mã Nhóm </th> 
        <th>Môn thể thao </th>
        <th>Nội dung thi đấu </th>
		<th>Sỉ số tối</th>
		<th>Sỉ số còn lại</th>
		<th>Nhập SĐT đăng ký</th>
       	<th>Chọn môn</th>

      </tr>
    </thead>
    <tbody>
		<?php $i=1;
		foreach($noidung as $nd){?>
      <tr>
	  	<td><?php echo $i++;?></td>
        <td><?php echo $nd['MANHOM'];?></td>
        <td><?php echo $nd['TENMH'];?></td>
		<td><?php echo $nd['TENNHOM'];?></td>
		<td><?php echo $nd['SISOTOIDA'];?></td>
		<td><?php echo $nd['SVDKT'];?></td>
		<td><input type="text" class="form-control" placeholder="Nhập số điện thoại!" name="sdtdk" id="sdtdk"></td>	
        <td>
		<?php 	
			$dk=false;
				foreach($kiemtra as $kt){
					if($kt['MANHOM']==$nd['MANHOM']){$dk=true;}	
				}
			if($dk) echo"Đã đăng kí" ;
			else if($nd['SVDKT'] < 1) echo"Đã đủ số lượng";
				else echo "<input name='chk[]'  value='" . $nd['MANHOM'] . "' class='chk' type='checkbox'/>";
		?>
		</td>
        <?php }?>

      </tr>
    </tbody>
    <tfoot>
      <tr>
      </tr>
    </tfoot>
  </table>
  <hr />
  <div class="form-group">
    <div class="col-sm-5"></div>
    <div class="col-sm-3">
      <button  class="form-control btn btn-primary btn-xs" id="AddMonTheThao" value="show"> Đăng ký </button>
    </div>
  </div>
</div>

